import 'dart:async';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class ImageUtil {
  static Future<ui.Image> getUIImage(String imageUrl,{int? width,int? height}) async {

    final ByteData assetImageByteData = await rootBundle.load(imageUrl);
    final codec = await ui.instantiateImageCodec(
      assetImageByteData.buffer.asUint8List(),
      targetHeight: height,
      targetWidth: width,
    );
    final image = (await codec.getNextFrame()).image;

    return image;
  }


  static List<List<Color>> transform2Square(List<List<Color>> imageData,{Color background = Colors.transparent}) {
    var list = imageData.toList();

    ///图片像素点长宽
    int imageWidth = list.length;
    int imageHeight = list.first.length;

    int diffCount;
    if(imageWidth > imageHeight){
      diffCount = imageWidth - imageHeight;

      int topCount = diffCount ~/2;
      int bottomCount = diffCount - topCount;
      ///转换成正方形图片
      for(int i = 0;i<list.length;i++ ){
        ///每列的顶部和底部补透明色
        list[i].insertAll(0,List.generate(topCount, (index) => background));
        list[i].addAll(List.generate(bottomCount, (index) => background));
      }
    }else if(imageWidth < imageHeight){
      diffCount = imageHeight - imageWidth;
      int leftCount = diffCount ~/2;
      int rightCount = diffCount - leftCount;
      list.insertAll(0, List.generate(leftCount, (index) => List.generate(imageHeight, (index) => background)));
      list.addAll(List.generate(rightCount, (index) => List.generate(imageHeight, (index) => background)));
    }

    return list;

  }

  // static List<List<Color>> transformSize(List<List<Color>> imageData, {required int size}){
  //
  //   if(imageData.length != imageData.first.length){
  //     throw Exception('The image data is not Square');
  //   }
  //
  //   int imageWidth = imageData.length;
  //   if(imageWidth > size){
  //     var preSize = imageWidth~/size;
  //     List<List<Color>> list = [];
  //     for(int i = 0;i<size;i++){
  //       for(int j = 0;j< size;j++){
  //
  //       }
  //     }
  //   }else{
  //
  //   }
  //
  // }
}